<?php

/**
 * This is the model class for table "{{article}}".
 *
 * The followings are the available columns in table '{{article}}':
 * @property integer $id
 * @property string $article_title
 * @property string $article_admin
 * @property string $article_content
 * @property integer $article_nav
 * @property integer $article_is_top
 * @property string $article_date
 * @property string $article_thumbnail
 * @property array $article_tag
 */
class Article extends CActiveRecord
{
	public $article_count;
	public $tags_str;

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{article}}';
	}

	public function rules()
	{
		return array(
			array('article_title, article_admin, article_content, article_summary', 'required'),
			array('article_thumbnail', 'file', 'allowEmpty'=>true, 'types'=>'jpg,png,gif','maxSize'=>1024*1024*10, 'tooLarge'=>'文件大于10M，上传失败！请上传小于10M的文件！'),
            array('article_nav', 'required', 'message'=>'所属栏目必须选择'),
			array('article_title', 'length', 'max'=>80),
			array('article_admin', 'length', 'max'=>6),
			array('article_summary', 'length', 'max'=>255),
			array('id, article_title, article_admin, article_content, article_nav, article_is_top, article_date, article_tag, article_thumbnail', 'safe'),
		);
	}

	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'article_title' => '文章标题',
			'article_admin' => '文章作者',
            'article_thumbnail' => '缩略图',
			'article_content' => '文章内容',
			'article_summary' => '文章摘要',
			'article_nav' => '所属栏目',
			'article_is_top' => '是否置顶',
			'article_date' => '发表日期',
			'article_tag' => '所属标签',
		);
	}

	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('article_title',$this->article_title,true);
		$criteria->compare('article_admin',$this->article_admin,true);
		$criteria->compare('article_content',$this->article_content,true);
		$criteria->compare('article_nav',$this->article_nav);
		$criteria->compare('article_is_top',$this->article_is_top);
		$criteria->compare('article_date',$this->article_date,true);
		$criteria->compare('article_tag',$this->article_tag);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

    // 在插入之前进行的操作
    public function beforeSave()
    {
        if($this->isNewRecord)
            $this->article_date = date('Y-m-d H:i:s');
        elseif(!$this->isNewRecord)
        {
            if($this->article_thumbnail == '')
            {
                $article = $this->findByPk($_GET['id']);
                $this->article_thumbnail = $article->article_thumbnail;
            }
        }
        if(sizeof($this->article_tag) > 1)
            $this->article_tag = implode(',', $this->article_tag);
        elseif(sizeof($this->article_tag) > 0)
            $this->article_tag = $this->article_tag[0];

        return true;
    }
}